package com.example.Activity;

import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fragment.HomeFragment;
import com.example.fragment.VideoFragment;

import de.hdodenhof.circleimageview.CircleImageView;

public class CateActivity extends AppCompatActivity implements View.OnClickListener {

    ImageView imageView1,imageView2;
    ClipData.Item collect,moneybag,loc,call,task;
    private DrawerLayout mDrawerLayout;
    HomeFragment hf=new HomeFragment();
    VideoFragment vf = new VideoFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cate_activity);
        initView();
        initBar();
        replaceFragment(hf);
        imageView1.setImageResource(R.drawable.fm_home1);

    }

    private void initView() {
        imageView1 = findViewById(R.id.imageView1);
        imageView2 = findViewById(R.id.imageView2);
        imageView1.setOnClickListener(this);
        imageView2.setOnClickListener(this);

    }

    private void initBar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawerLayout = findViewById(R.id.drawer_layout);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        NavigationView navView = findViewById(R.id.nav_view);

        View header=navView.inflateHeaderView(R.layout.nav_header);//
        CircleImageView head=  header.findViewById(R.id.icon_image);
        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CateActivity.this,"image",Toast.LENGTH_SHORT).show();
            }
        });

        TextView username =header.findViewById(R.id.username);
        Intent intent =getIntent();
        username.setText(intent.getStringExtra("username"));
        username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CateActivity.this,"username",Toast.LENGTH_SHORT).show();
            }
        });


//        username.setText = intent.getStringExtra("username");

//private  NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
//headerLayout = navigationView.inflateHeaderView(R.layout.nav_header_main);
// head = (ImageView) headerLayout.findViewById(R.id.Iamge_mine_head);
//  myName = (TextView) headerLayout.findViewById(R.id.Tview_mine_myName);
//     要用   View headerView = navigationView.getHeaderView(0);

//        navView.getHeaderView(0).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(CateActivity.this,"Pic",Toast.LENGTH_SHORT).show();
//            }
//        });

        navView.setCheckedItem(R.id.nav_task);
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int id = menuItem.getItemId();
                if (id == R.id.nav_moneybag) {
                    // Handle the camera action
                    Toast.makeText(CateActivity.this,"You clicked nav_moneybag",Toast.LENGTH_SHORT).show();
                } else if (id == R.id.nav_collection) {
                    Toast.makeText(CateActivity.this,"You clicked nav_collection",Toast.LENGTH_SHORT).show();
                } else if (id == R.id.nav_location) {
                    Intent intent = new Intent(CateActivity.this,BaiduMapActivity.class);
                    startActivity(intent);
//                    finish();
                    Toast.makeText(CateActivity.this,"You clicked nav_location",Toast.LENGTH_SHORT).show();
                } else if (id == R.id.nav_task) {
                    Intent intent4 = new Intent(CateActivity.this,WebViewActivity.class);
                    startActivity(intent4);
                    Toast.makeText(CateActivity.this,"You clicked nav_call",Toast.LENGTH_SHORT).show();
                } else if (id == R.id.nav_call) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:10086"));
                    Toast.makeText(CateActivity.this,"You clicked nav_task",Toast.LENGTH_SHORT).show();
                } else if (id == R.id.nav_exit) {
                    finish();
                    Toast.makeText(CateActivity.this,"You clicked nav_exit",Toast.LENGTH_SHORT).show();

                }
                mDrawerLayout.closeDrawers();
                return true;
            }

        });
    }


    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.location:
                Intent intent = new Intent(CateActivity.this,BaiduMapActivity.class);
                startActivity(intent);
//                finish();
                Toast.makeText(CateActivity.this,"You clicked Location",Toast.LENGTH_SHORT).show();
                break;
            case R.id.camera:
                Intent intent3 = new Intent(CateActivity.this,CameraAlbumActivity.class);
                startActivity(intent3);
//                finish();
                Toast.makeText(CateActivity.this,"You clicked Camera",Toast.LENGTH_SHORT).show();
                break;
            case R.id.settings:
                Intent intent4 = new Intent(CateActivity.this,WebViewActivity.class);
                startActivity(intent4);
                Toast.makeText(CateActivity.this,"You clicked Settings",Toast.LENGTH_SHORT).show();
                break;
                default:
                    break;
        }
        return true;
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_content,fragment);
        ft.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView1:
                replaceFragment(hf);
                imageView1.setImageResource(R.drawable.fm_home1);
                imageView2.setImageResource(R.drawable.fm_video);
                Toast.makeText(CateActivity.this,"home",Toast.LENGTH_SHORT).show();
                break;
            case R.id.imageView2:
                replaceFragment(vf);
                imageView1.setImageResource(R.drawable.fm_home);
                imageView2.setImageResource(R.drawable.fm_video1);

                Toast.makeText(CateActivity.this,"video",Toast.LENGTH_SHORT).show();
                break;

            default:
                break;
        }
    }

}
